//给你二叉树的根节点 root 和一个整数目标和 targetSum ，找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。 
//
// 叶子节点 是指没有子节点的节点。 
//
// 
// 
// 
//
// 示例 1： 
//
// 
//输入：root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
//输出：[[5,4,11,2],[5,8,4,5]]
// 
//
// 示例 2： 
//
// 
//输入：root = [1,2,3], targetSum = 5
//输出：[]
// 
//
// 示例 3： 
//
// 
//输入：root = [1,2], targetSum = 0
//输出：[]
// 
//
// 
//
// 提示： 
//
// 
// 树中节点总数在范围 [0, 5000] 内 
// -1000 <= Node.val <= 1000 
// -1000 <= targetSum <= 1000 
// 
// 
// 
// Related Topics 树 深度优先搜索 回溯 二叉树 👍 851 👎 0


package com.cjl.leetcode.editor.cn;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * [P113]_路径总和 II
 * @author cjl
 * @date 2022-10-08 20:01:20
 */
public class P113_PathSumIi{
      public static void main(String[] args) {
            //测试代码
           Solution solution = new P113_PathSumIi().new Solution();
      }
      //力扣代码
      //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    /**
     * 方法二：递归
     * @param root
     * @param targetSum
     * @return
     */
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> resList = new ArrayList<>();
        if(null == root){
            return resList;
        }
        Deque<Integer> currPath = new ArrayDeque<>();
        dfs(root, targetSum, resList, currPath);
        return resList;
    }

    public void dfs(TreeNode node , int targetSum, List<List<Integer>> resList,Deque<Integer> currPath){
        if(null == node){
            return;
        }
        targetSum -= node.val;
        currPath.addLast(node.val);
        //入最终结果条件
        if(null == node.left && null == node.right && targetSum == 0){
            resList.add(new ArrayList<>(currPath));
        }
        //左分支
        dfs(node.left, targetSum, resList, currPath);
        //右分支
        dfs(node.right, targetSum, resList, currPath);
        //结束当前路径判断后，弹出写入的节点
        currPath.pollLast();
        //恢复targetSum
        targetSum += node.val;
    }

    /**
     * 方法一：
     * @param root
     * @param targetSum
     * @return
     */
    public List<List<Integer>> pathSum2(TreeNode root, int targetSum) {
        List<List<Integer>> resList = new ArrayList<>();
        if(null == root){
            return resList;
        }
        Deque<TreeNode> deque = new ArrayDeque<>();
        Deque<List<Integer>> roadDeque = new ArrayDeque<>();
        deque.addFirst(root);
        List<Integer> treeNodeList = new ArrayList<>();
        treeNodeList.add(root.val);
        roadDeque.addFirst(treeNodeList);
        while (!deque.isEmpty()){
            int size = deque.size();
            for(int i = 0 ; i < size ; i ++){
                TreeNode tempNode = deque.pollLast();
                List<Integer> tempList = roadDeque.pollLast();
                if(null == tempNode.left && null == tempNode.right && tempNode.val == targetSum){
                    resList.add(tempList);
                }
                if(null != tempNode.left){
                    List<Integer> leftList = new ArrayList<>();
                    leftList.addAll(tempList);
                    leftList.add(tempNode.left.val);
                    roadDeque.addFirst(leftList);
                    tempNode.left.val = tempNode.val + tempNode.left.val;
                    deque.addFirst(tempNode.left);
                }
                if(null != tempNode.right){
                    List<Integer> rightList = new ArrayList<>();
                    rightList.addAll(tempList);
                    rightList.add(tempNode.right.val);
                    roadDeque.addFirst(rightList);
                    tempNode.right.val = tempNode.val + tempNode.right.val;
                    deque.addFirst(tempNode.right);
                }
            }
        }
        return resList;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

  }